package com.Hsu.book.controller;

import com.Hsu.book.enums.ResultCode;
import com.Hsu.book.model.*;
import com.Hsu.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Hsu琛君珩
 * @ClassName:BookInfoController
 * @date 2024-01-04
 * @apiNote
 * @Version: v1.0
 */

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired//告诉Spring，从容器中取出这个对象，赋值给当前对象的属性
    private BookService bookService;
    //等价于
    //private BookService bookService
    //public BookController() {
    //    this.bookService = new BookService();
    //}

    @RequestMapping("/getBookListByPage")
    //为了方便更好拓展，最好返回结果也是一个对象
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询翻页信息，pageRequest:{}", pageRequest);
        ////用户登录校验
        //UserInfo userInfo= (UserInfo) session.getAttribute("session_user_key");
        //if(userInfo==null||userInfo.getId()<=0||"".equals(userInfo.getUserName())){
        //    //用户未登录
        //    return Result.unLogin();
        //}
        //校验成功
        if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            //每页显示条数为负或者当前页数不为正数则错误
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        } catch (Exception e) {
            log.error("查询翻页信息错误，e:{}", e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接收到添加图书请求，bookInfo:{}", bookInfo);
        //参数校验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数校验失败，请检查入参";
        }
        Integer result = bookService.addBook(bookInfo);
        if (result <= 0) {
            //这部分日志打印是方便调试的
            log.error("添加图书出错:bookInfo:{}", bookInfo);
            //因为Controller是对外服务的，返回时不要返回太专业的内容
            return "添加图书出错，请联系管理员";
        }
        return "";
    }

    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId) {
        log.info("根据ID查询图书，bookId:{}", bookId);
        //long start=System.currentTimeMillis();
        BookInfo bookInfo =null;
        try {
            bookInfo = bookService.queryBookInfoById(bookId);
        } catch (Exception e) {
            log.error("查询图书失败，e:{}", e);
        }
        //long end=System.currentTimeMillis();
        //log.info("queryBookInfoById执行耗时："+(end-start)+"ms");
        return bookInfo;
    }

    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接收到更新图书的请求，bookInfo:{}", bookInfo);
        Integer result = bookService.updateBook(bookInfo);
        if (result == 0) {
            log.error("更新图书失败，bookInfo:{}", bookInfo);
            return "更新图书失败，请联系管理员";
        }
        return "";
    }

    @RequestMapping(value = "/batchDelete", produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        log.info("接受批量删除的请求，图书id:{}", ids);
        Integer result = bookService.batchDelete(ids);
        if (result <= 0) {
            log.error("批量删除失败，ids:{}", ids);
            return "批量删除失败，请联系管理员";
        }
        return "";
    }
}